package com.liser.southernstar.update.install;


import android.util.Log;

import com.liser.southernstar.MainApplication;
import com.liser.southernstar.update.UpdateServerUrls;
import com.liser.southernstar.utils.ApkUtils;
import com.liser.southernstar.utils.DownloadUtils;

import java.io.File;
import java.io.IOException;

/**
 * 此类下载apk并根据设备型号选择安装方式
 */
public class DefaultAppInstaller {

    private static final String TAG = DefaultAppInstaller.class.getName();

    /**
     * 下载缓存，64kb
     */
    private static final int DOWNLOAD_BUFFER_SIZE = 1024 * 64;

    /**
     * 下载apk文件并安装
     */
    public static void downloadAndInstall(String versionName) throws IOException {
        // 下载文件
//        ToastHelper.show("正在下载更新：" + versionName);
        File file = DownloadUtils.copyFile(MainApplication
                        .instance()
                        .getCacheDir()
                        .getPath()
                        + "/app-debug-" + versionName + ".apk"
                , new UpdateServerUrls("/app-debug-" + versionName + ".apk")
                , DOWNLOAD_BUFFER_SIZE
                , 3600000
        );
        // 等待支付空闲时安装
        /*FacePayStatus status;
        for (; ; ) {
            status = FacePayHelper.getStatus();
            if (status != FacePayStatus.Free) {
                try {
                    Thread.sleep(1000);
                } catch (Exception ex) {
                }
                continue;
            }
            ToastHelper.show("正在安装更新：" + versionName);
            ApkUtils.intentInstall(file, MainApplication.instance().getApplicationContext().getPackageName() + ".fileprovider");
            break;
        }*/

//        ToastHelper.show("正在安装更新：" + versionName);
        ApkUtils.intentInstall(file, MainApplication.instance().getPackageName() + ".fileprovider");
        Log.e(TAG, "APK安装成功");
    }
}
